package com.wfl.sampling.service.impl;

import com.aliyun.oss.OSS;
import com.wfl.sampling.anno.Log;
import com.wfl.sampling.aop.RunTimeGet;
import com.wfl.sampling.service.LoadFileService;
import com.wfl.sampling.utils.AIiOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LoadFileServiceImpl implements LoadFileService {
    @Autowired
    private AIiOSSUtils aIiOSSUtils;
//    @Override
//    public List<String> loadFiles(List<MultipartFile> multipartFiles) {
//        Iterator<MultipartFile> image = multipartFiles.iterator();
//        List<String> imagePaths = new ArrayList<>();
//        while(image.hasNext()){
//            try {
//                imagePaths.add(aIiOSSUtils.upload((MultipartFile) image));
//            } catch (IOException e) {
//                log.info("上传错误");
//                return null;
//            }
//        }
//        return imagePaths;
//    }
//    @RunTimeGet
//    @Override
//    public List<String> loadFiles(List<MultipartFile> multipartFiles) {
////        Iterator<MultipartFile> iterator = multipartFiles.iterator();
////        List<String> imagePaths = new ArrayList<>();
////
////        while (iterator.hasNext()) {
////            MultipartFile file = iterator.next();
////            try {
////                // 假设 aIiOSSUtils.upload 返回文件路径
////                String imagePath = aIiOSSUtils.upload(file);
////                imagePaths.add(imagePath);
////            } catch (IOException e) {
////                log.info("上传错误");
////                // 处理上传错误的情况
////                return null;
////            }
////        }
////        List<String> imagePaths = new ArrayList<>();
////        for (MultipartFile multipartFile : multipartFiles) {
////            try {
////                imagePaths.add(aIiOSSUtils.upload(multipartFile));
////            } catch (IOException e) {
////                log.info("文件错误");
////            }
////        }
////        return imagePaths;
//        try {
//            List<CompletableFuture<String>> futures = multipartFiles.stream()
//                    .map(this::uploadFileAsync)
//                    .collect(Collectors.toList());
//
//            // 等待所有异步任务完成，并收集结果
//            return futures.stream()
//                    .map(CompletableFuture::join)
//                    .collect(Collectors.toList());
//        } catch (Exception e) {
//            log.info("多次传输文件失败");
//        } finally {
//            aIiOSSUtils.shutdown();
//        }
//        return null;
//    }
//
//
//
//    private CompletableFuture<String> uploadFileAsync(MultipartFile file) {
//        return CompletableFuture.supplyAsync(() -> {
//            try {
//                return aIiOSSUtils.upload(file);
//            } catch (IOException e) {
//                log.info("文件错误");
//                return null;  // 或者根据需要处理错误情况
//            }
//        });
//    }


//    private static final int THREAD_POOL_SIZE = 10; // 可以根据需要调整线程池大小
//    private final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);

//    @RunTimeGet
//    @Override
//    public List<String> loadFiles(List<MultipartFile> multipartFiles) {
//        List<CompletableFuture<String>> futures = new ArrayList<>();
//        for (MultipartFile multipartFile : multipartFiles) {
//            futures.add(CompletableFuture.supplyAsync(() -> {
//                try {
//                    return aIiOSSUtils.upload(multipartFile);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }, executorService));
//        }
//        return futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
//    }

    //
    @RunTimeGet
    @Override
    public List<String> loadFiles(List<MultipartFile> multipartFiles){
        List<String> filePaths = new ArrayList<>();
        try {
            for (MultipartFile multipartFile : multipartFiles){
                filePaths.add(aIiOSSUtils.upload(multipartFile));
            }
            return filePaths;
        } catch (IOException e) {
            log.info("传输多个文件失败！");
        }
        return null;
    }


    @Override
    public String newLoadFiles(MultipartFile multipartFile) {
        try {
            return aIiOSSUtils.upload(multipartFile);
        } catch (Exception e) {
            log.info("文件传输错误！");
            return null;
        }

    }


}
